package com.shen.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shen.commonutils.Result;
import com.shen.eduservice.client.VodClient;
import com.shen.eduservice.entity.EduVideo;
import com.shen.eduservice.service.EduVideoService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author shenkaituo
 * @since 2020-06-21
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private VodClient vodClient;
    //添加小节
    @PostMapping("addVideo")
    public Result addVideo(@RequestBody  EduVideo eduVideo){
        System.out.println(eduVideo);
        boolean save = eduVideoService.save(eduVideo);
        if(save) return Result.ok();
        else return Result.error();
    }
    //删除小节
    //后面完善在删除小节的同时，把视频也删除
    @DeleteMapping("{videoId}")
    public  Result deleteVideo(@PathVariable String videoId){
        System.out.println("////////////////////////////////////////////");
        System.out.println("delete video controller");
        //根据小节id得到视频ID
        EduVideo video = eduVideoService.getById(videoId);
        String videoSourceId=video.getVideoSourceId();

        if(!StringUtils.isEmpty(videoSourceId)){
            vodClient.removeVideo(videoSourceId);
        }
            //先删除阿里云视频再删除小节 删除小节之后 视频id就查不到了
        boolean flag = eduVideoService.removeById(videoId);
        if(flag) return Result.ok();
        else  return  Result.error();
    }
    //修改小节
    @PostMapping("updateVideo")
    public  Result updateVideo(@RequestBody EduVideo eduVideo){
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        System.out.println("updateVideo");

        boolean update = eduVideoService.updateById(eduVideo);
        if(update) return Result.ok();
        else return  Result.error();

    }

}

